package net.enclosing.basecamp;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.time.DateUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Basecamp {
	public static void main(String[] args) {
		testConnection();
	}
	public static Document get(String path){

		LocalStaticData staticData = new LocalStaticData();
		HttpClient client = new HttpClient();
		client.getParams().setAuthenticationPreemptive(true);
//		Credentials defaultcreds1 = new UsernamePasswordCredentials(staticData.getAPI_KEY(), "X");
		Credentials defaultcreds1 = new UsernamePasswordCredentials("tkb", "aiueo0101");
		AuthScope scope1 = new AuthScope("storytellers.basecamphq.com", 443);
		client.getState().setCredentials(scope1, defaultcreds1);
		String url = staticData.getURL()+path;
		System.err.println(url);;
		HttpMethod method = new GetMethod(staticData.getURL()+path);
		method.setDoAuthentication(true);
		try {
			int statusCode = client.executeMethod(method);
			String responseBodyAsString =  method.getResponseBodyAsString();
			Document document =  DocumentHelper.parseText(responseBodyAsString);
			return document;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void testConnection(){
		final String path = "account.xml";
		Document document = get(path);
		Element rootElement = document.getRootElement();
		System.err.println(rootElement.getName());


	}
	public static Date recentThresholdDate() {
		LocalStaticData staticData = new LocalStaticData();
		
		Date recentThresholdDate = DateUtils.addDays(new Date(), staticData.getRecentThresholdDays());
		recentThresholdDate = DateUtils.truncate(recentThresholdDate, Calendar.DAY_OF_MONTH);
		return recentThresholdDate;
	}

}
